package com.example.mobilesafe.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.utils.ToastUtil;

public class EnterPsdActivity extends AppCompatActivity {

    private String packagename;

    private TextView tv_app_name;

    private ImageView iv_app_icon;

    private EditText et_psd;

    private Button btn_submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_psd);
        // 获取包名
        packagename = getIntent().getStringExtra("packagename");

        // 初始化UI
        initUI();

        // 初始化数据
        initData();
    }

    /**
     * 初始化UI
     */
    private void initUI() {
        tv_app_name = findViewById(R.id.tv_app_name);
        iv_app_icon = findViewById(R.id.iv_app_icon);
        et_psd = findViewById(R.id.et_psd);
        btn_submit = findViewById(R.id.btn_submit);
    }

    /**
     * 通过传递过来的包名获取拦截应用的图标以及名称
     */
    private void initData() {
        PackageManager packageManager = getPackageManager();
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packagename,0);
            Drawable icon = applicationInfo.loadIcon(packageManager);
            String label = applicationInfo.loadLabel(packageManager).toString();
            iv_app_icon.setBackgroundDrawable(icon);
            tv_app_name.setText(label);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String psd = et_psd.getText().toString();
                if (!TextUtils.isEmpty(psd)){
                    if (psd.equals("123")){
                        // 解锁，进入应用，告知看门狗服务不需要再去监听已经解锁的应用，需要发送广播
                        Intent intent = new Intent("android.intent.action.SKIP");
                        intent.putExtra("packagename",packagename);
                        sendBroadcast(intent);
                        finish();
                    }else {
                        ToastUtil.show(getApplicationContext(),"密码错误！");
                    }
                }else {
                    ToastUtil.show(getApplicationContext(),"请输入密码！");
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // 跳转到桌面
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }
}